---------------------------------------------------------
-- 用于扩展lua库
---------------------------------------------------------

-- 拆分字符串
function string.split(Txt, Char)
	local step = string.len(Char)
	local S = 1
	local E
	local Ret = {}
	if string.len(Txt)>0 then
		while true do
			E = string.find(Txt, Char, S)
			Ret[#Ret+1] = string.sub(Txt, S, E and E - 1)
			if not E then break end
			S = E + step
		end
	end
	return Ret
end

--切割根据字符串
function string.splitbystr(Txt, str)
	local S = 1
	local E
	local Ret = {}
	while true do
		E = string.find(Txt, str, S)
		local T = string.sub(Txt, S, E and E - 1)
		if T~="" then Ret[#Ret+1] = T end
		if not E then break end
		S = E + string.len(str)
	end
	return Ret
end
	
-- 靠左显示字符
function string.align_left(Str, MaxLen)
	local Fmt = "%-"..MaxLen.."s"
	return Fmt:format(Str)
end

-- 靠右显示字符
function string.align_right(Str, MaxLen)
	local Fmt = "%"..MaxLen.."s"
	return Fmt:format(Str)
end

-- 居中显示字符串
function string.align_center(Str, MaxLen)
	local Len = string.len(Str)
	if Len >= MaxLen then return Str end
	Str = string.align_right(Str, math.floor((MaxLen - Len) / 2) + Len)
	return string.align_left(Str, MaxLen)
end

function string.trim(s) 
  return (string.gsub(s, "^%s*(.-)%s*$", "%1")) 
end 

string.startswith = function(self,s2)
    return string.find(self,s2,1,true) == 1
end

string.endswith = function(s,send)
 	return #s >= #send and s:find(send, #s-#send+1, true) and true or false
end